package com.fwmagic.flink.state;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.common.typeinfo.TypeHint;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 * 通过keyedState实现sum的效果
 */
public class MapWithKeyStateV2 {
    public static void main(String[] args) throws Exception{
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        //设置Checkpointing，程序挂了会自动重启（不然不会重启），state默认保存在内存中，默认会无限重启，重启后内存中数据仍然保留
        //最好保存在外部分布式存储系统中
        env.enableCheckpointing(5000);

        DataStreamSource<String> dataStream = env.socketTextStream("localhost", 8888);

        SingleOutputStreamOperator<Tuple2<String, Integer>> wordAndOne = dataStream.flatMap(new FlatMapFunction<String, Tuple2<String, Integer>>() {
            @Override
            public void flatMap(String line, Collector<Tuple2<String, Integer>> out) throws Exception {
                String[] words = line.split("\\s");
                for (String word : words) {
                    out.collect(Tuple2.of(word, 1));
                }
            }
        });

        //分组
        KeyedStream<Tuple2<String, Integer>, Tuple> keyedStream = wordAndOne.keyBy(0);

        //自定义ValueState求和
        SingleOutputStreamOperator<Tuple2<String, Integer>> sumed = keyedStream.map(new RichMapFunction<Tuple2<String, Integer>, Tuple2<String, Integer>>() {

            private transient ValueState<Integer> valueState;

            @Override
            public void open(Configuration parameters) throws Exception {
                //定义ValueStateDescriptor
                ValueStateDescriptor<Integer> valueStateDescriptor = new ValueStateDescriptor<Integer>(
                        "wc-state-v2",
                        TypeInformation.of(new TypeHint<Integer>() {
                        }));

                //获得valueState
                valueState = getRuntimeContext().getState(valueStateDescriptor);
            }

            @Override
            public Tuple2<String, Integer> map(Tuple2<String, Integer> tuple) throws Exception {
                String word = tuple.f0;
                Integer count = tuple.f1;
                if (word.equals("duanwang")) {
                    throw new RuntimeException("断网了，程序异常了！");
                }
                //通过valueState获取状态值：第一次为空，后面获取的为历史记录值
                Integer histroyValue = valueState.value();
                if(histroyValue ==null){
                    //更新state
                    valueState.update(count);
                    //返回
                    return tuple;
                }else{
                    histroyValue+=count;
                    return Tuple2.of(word, histroyValue);
                }

                /*if (histroyValue != null) {
                    //累加
                    tuple.f1 += histroyValue;
                }
                //更新状态值
                valueState.update(tuple.f1);
                return tuple;
                */

            }
        });

        sumed.print();

        env.execute("MapWithKeyStateV1");

    }
}
